/**
 * 文件名：HUtil.java
 *
 * 版本信息：
 * 日期：2009-6-17
 * Copyright dp Corporation 2009 
 * 版权所有 2009All rights reserved.
 * 创建者：wht
 *
 */
package com.dp.pub;

import java.sql.Connection;

import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
 * 
 * 项目名称：book
 * 类名称：HUtil
 * 类描述：全局的工具类，用来获取数据库连接，SessionFactory，EntityManagerFactory
 * 创建人：Administrator
 * 创建时间：2009-6-17 下午02:16:12
 * 修改人：Administrator
 * 修改时间：2009-6-17 下午02:16:12
 * 修改备注：
 * @version 
 * 
 */
public class HUtil {

	//private static HashMap sessionFactoryList = new HashMap();

	//public static final ThreadLocal session = new ThreadLocal();

//	private static Session ss = null;

//	private static SessionFactory sf = null;

	private static InitialContext ctx = null;
	private static DataSource rptds  = null;
//	public static void initSessionFactory(SessionFactory sft) {
//		sf = sft;
//	}
//
//	public static SessionFactory getSessionFactory() {
//		if (sf == null) {
//			try {
//				ctx = new InitialContext();
//				sf = (SessionFactory) ctx
//						.lookup("java:comp/env/hibernate/SessionFactory");
//				return sf;
//				//return new Configuration().configure(sessionFactoryId). buildSessionFactory();
//			} catch (Exception ex) {
//				throw new RuntimeException(
//						"Exception building SessionFactory: " + ex.getMessage(),
//						ex);
//			}
//		} else {
//			return sf;
//		}
//	}
//
//	public static Session currentSession() throws HibernateException {
//		Session s = (Session) session.get();
//		if (s == null) {
//			try {
//				ctx = new InitialContext();
//				sf = (SessionFactory) ctx
//						.lookup("java:comp/env/hibernate/SessionFactory");
//				s = sf.openSession();
//			} catch (NamingException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//
//			session.set(s);
//		}
//		return s;
//	}
//
//	public static void closeSession() throws HibernateException {
//		Session s = (Session) session.get();
//		session.set(null);
//
//		if (s != null) {
//			if (s.isOpen())
//				s.close();
//			s = null;
//		}
//	}
//	 private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("mySQLVOPU");
//	 public static EntityManagerFactory getEMF(){ return emf;}
	public static Connection getConn() {
		Connection conn = null;
		try {
			//InitialContext ctx = new InitialContext();
			//System.getProperty("jboss.server.home.url");
			//DataSource ds = (DataSource) ctx.lookup((System.getProperty("mydatasource")));
			//conn = ds.getConnection();
			//conn.setAutoCommit(false);
			conn =org.springframework.jdbc.datasource.DataSourceUtils.getConnection((DataSource) MyServer.ctx.getBean("dataSource"));
			return conn;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		//DataSourceUtils.closeConnectionIfNecessry(connection, dataSource);

		
	}

		public static  Connection getConn(String dsjndi) {
	          Connection conn1=null;
		
		try {
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx.lookup(dsjndi);
			conn1 = ds.getConnection();
			conn1.setAutoCommit(false);
			return conn1;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		
	}
		
		public static Connection getRptMConn() {
			Connection conn = null;
			try {
				if(rptds==null);
				{
					InitialContext ctx = new InitialContext();
				    //System.getProperty("jboss.server.home.url");
					rptds = (DataSource) ctx.lookup((System.getProperty("myrptdatasource")));
				}
				return rptds.getConnection();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return conn;
		}
}
